<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<h1>Test that pointer properties propagates from touches to PointerEvents</h1>
<div id="target0" style="height: 100px;"></div>
<script>

// TODO(crbug.com/1076938): This test cannot be automated using current test_driver APIs.

window.name = "mainWindow";
async_test(function() {
  if (!window.eventSender) {
    this.done();
    return;
  }
  var POINTER_PROPERTIES = [
    { width: 1.125, height: 1.25, pressure: .1875, tiltX: 0, tiltY: 0, pointerType: "mouse", isPrimary: true },
    { width: 2.125, height: 2.25, pressure: .2500, tiltX: 25, tiltY: 26, pointerType: "pen", isPrimary: true },
    { width: 3.125, height: 3.25, pressure: .3125, tiltX: 0, tiltY: 0, pointerType: "touch", isPrimary: true },
    { width: 4.125, height: 4.25, pressure: .4375, tiltX: 0, tiltY: 0, pointerType: "touch", isPrimary: false }
  ];
  var receivedPointerEvents = [];
  var target0 = document.getElementById("target0");
  function checkPointerEvent(event) {
    receivedPointerEvents.push(event);
    test(function() {
      assert_between_inclusive(event.pointerId, 1, POINTER_PROPERTIES.length);
      var pp = POINTER_PROPERTIES[event.pointerId-1];
      for (var i in pp)
        assert_equals(event[i], (event.type == "pointerup" && i == "pressure") ? 0 : pp[i], "" + i);
      assert_equals(event.view.name, "mainWindow");
    }, "Pointer event properties for pointer " + event.pointerId + " on " + event.type);
  }
  on_event(target0, "pointerdown", this.step_func(checkPointerEvent));
  on_event(target0, "pointermove", this.step_func(checkPointerEvent));
  on_event(target0, "pointerup", this.step_func(checkPointerEvent));
  on_event(window, "load", this.step_func_done(function() {
    assert_not_equals(window.PointerEvent, undefined, "window.PointerEvent");
    var x = target0.offsetLeft + 2;
    var y = target0.offsetTop + 2;

    for (var i = 0 ; i < POINTER_PROPERTIES.length; ++i) {
      var pp = POINTER_PROPERTIES[i];
      if (i != POINTER_PROPERTIES.length-1)
        eventSender.addTouchPoint(x + i, y + i, pp["width"] / 2.0, pp["height"] / 2.0, pp["pressure"], pp["tiltX"], pp["tiltY"], pp["pointerType"]);
      else // Test default pointerType
        eventSender.addTouchPoint(x + i, y + i, pp["width"] / 2.0, pp["height"] / 2.0, pp["pressure"], pp["tiltX"], pp["tiltY"]);
    }
    eventSender.touchStart();

    for (var i = 0 ; i < POINTER_PROPERTIES.length; ++i) {
      var pp = POINTER_PROPERTIES[i];
      eventSender.updateTouchPoint(i, x + i + 20, y + i + 20);
    }

    eventSender.touchMove();
    for (var i = 0 ; i < POINTER_PROPERTIES.length ; ++i)
      eventSender.releaseTouchPoint(i);
    eventSender.touchEnd();

    test(function() {
      assert_array_equals(
        receivedPointerEvents.map(function(event) { return event.type; }),
        ["pointerdown", "pointerdown", "pointerdown", "pointerdown",
         "pointermove", "pointermove", "pointermove", "pointermove",
         "pointerup", "pointerup", "pointerup", "pointerup"]);
    }, "Received pointer events");
  }));
}, "Pointer property propagation from touches to PointerEvents");
</script>
